//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "IntegralHestonVarianceOptionEngine.h"
using namespace Cephei::QL::Experimental::Varianceoption;
#include <gen/QL/Processes/HestonProcess.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL::Processes;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Varianceoption::CIntegralHestonVarianceOptionEngine::CIntegralHestonVarianceOptionEngine (Cephei::QL::Processes::IHestonProcess^ process) : CPricingEngine(CIntegralHestonVarianceOptionEngine::typeid)
{
    CHestonProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phIntegralHestonVarianceOptionEngine = NULL;
#endif
        _Cprocess = safe_cast<CHestonProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::HestonProcess>& _process = static_cast<boost::shared_ptr<QuantLib::HestonProcess>&> (_Cprocess->GetShared ()); 
        _ppIntegralHestonVarianceOptionEngine = new boost::shared_ptr<QuantLib::IntegralHestonVarianceOptionEngine> (new QuantLib::IntegralHestonVarianceOptionEngine ( _process ));
        SetPricingEngine (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppIntegralHestonVarianceOptionEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Experimental::Varianceoption::CIntegralHestonVarianceOptionEngine::CIntegralHestonVarianceOptionEngine (boost::shared_ptr<QuantLib::IntegralHestonVarianceOptionEngine>& childNative, Object^ owner) : CPricingEngine(CIntegralHestonVarianceOptionEngine::typeid)
{
#ifdef HANDLE
	_phIntegralHestonVarianceOptionEngine = NULL;
#endif
	_ppIntegralHestonVarianceOptionEngine = &childNative;
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppIntegralHestonVarianceOptionEngine));
}
Cephei::QL::Experimental::Varianceoption::CIntegralHestonVarianceOptionEngine::CIntegralHestonVarianceOptionEngine (QuantLib::IntegralHestonVarianceOptionEngine& childNative, Object^ owner) : CPricingEngine(CIntegralHestonVarianceOptionEngine::typeid)
{
#ifdef HANDLE
	_phIntegralHestonVarianceOptionEngine = NULL;
#endif
	_ppIntegralHestonVarianceOptionEngine = new boost::shared_ptr<QuantLib::IntegralHestonVarianceOptionEngine> (&childNative);
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppIntegralHestonVarianceOptionEngine));
    _IntegralHestonVarianceOptionEngineOwner = owner;
    _PricingEngineOwner = owner;
}

Cephei::QL::Experimental::Varianceoption::CIntegralHestonVarianceOptionEngine::CIntegralHestonVarianceOptionEngine (CIntegralHestonVarianceOptionEngine^ copy) : CPricingEngine(CIntegralHestonVarianceOptionEngine::typeid)
{
#ifdef HANDLE
	_phIntegralHestonVarianceOptionEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppIntegralHestonVarianceOptionEngine = new boost::shared_ptr<QuantLib::IntegralHestonVarianceOptionEngine> (copy->GetShared());
        _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppIntegralHestonVarianceOptionEngine));
    }
}
Cephei::QL::Experimental::Varianceoption::CIntegralHestonVarianceOptionEngine::CIntegralHestonVarianceOptionEngine (System::Type^ t) : CPricingEngine(CIntegralHestonVarianceOptionEngine::typeid)
{
#ifdef HANDLE
	_phIntegralHestonVarianceOptionEngine = NULL;
#endif
	if (!t->IsSubclassOf(CIntegralHestonVarianceOptionEngine::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Varianceoption::CIntegralHestonVarianceOptionEngine::CIntegralHestonVarianceOptionEngine (QuantLib::Handle<QuantLib::IntegralHestonVarianceOptionEngine>& childNative, Object^ owner)  : CPricingEngine(CIntegralHestonVarianceOptionEngine::typeid)
{
	_phIntegralHestonVarianceOptionEngine = &childNative;
	_ppIntegralHestonVarianceOptionEngine = &static_cast<boost::shared_ptr<QuantLib::IntegralHestonVarianceOptionEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppIntegralHestonVarianceOptionEngine));
    _IntegralHestonVarianceOptionEngineOwner = owner;
}
Cephei::QL::Experimental::Varianceoption::CIntegralHestonVarianceOptionEngine::CIntegralHestonVarianceOptionEngine (QuantLib::Handle<QuantLib::IntegralHestonVarianceOptionEngine> childNative)  : CPricingEngine(CIntegralHestonVarianceOptionEngine::typeid)
{
	_phIntegralHestonVarianceOptionEngine = &childNative;
	_ppIntegralHestonVarianceOptionEngine = &static_cast<boost::shared_ptr<QuantLib::IntegralHestonVarianceOptionEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppIntegralHestonVarianceOptionEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Varianceoption::CIntegralHestonVarianceOptionEngine::CIntegralHestonVarianceOptionEngine (QuantLib::IntegralHestonVarianceOptionEngine childNative)  : CPricingEngine(CIntegralHestonVarianceOptionEngine::typeid)
{
#ifdef HANDLE
	_phIntegralHestonVarianceOptionEngine = NULL;
#endif
	_ppIntegralHestonVarianceOptionEngine = new boost::shared_ptr<QuantLib::IntegralHestonVarianceOptionEngine> (new QuantLib::IntegralHestonVarianceOptionEngine (childNative));
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppIntegralHestonVarianceOptionEngine));
}
#endif

Cephei::QL::Experimental::Varianceoption::CIntegralHestonVarianceOptionEngine::~CIntegralHestonVarianceOptionEngine ()
{
    if (_ppIntegralHestonVarianceOptionEngine != NULL)
    {
	    delete _ppIntegralHestonVarianceOptionEngine;
        _ppIntegralHestonVarianceOptionEngine = NULL;
    }
}
Cephei::QL::Experimental::Varianceoption::CIntegralHestonVarianceOptionEngine::!CIntegralHestonVarianceOptionEngine ()
{
    if (_ppIntegralHestonVarianceOptionEngine != NULL)
    {
	    delete _ppIntegralHestonVarianceOptionEngine;
    }
}
QuantLib::IntegralHestonVarianceOptionEngine& Cephei::QL::Experimental::Varianceoption::CIntegralHestonVarianceOptionEngine::GetReference ()
{
    if (_ppIntegralHestonVarianceOptionEngine == NULL) throw gcnew NativeNullException ();
	return **_ppIntegralHestonVarianceOptionEngine;
}
boost::shared_ptr<QuantLib::IntegralHestonVarianceOptionEngine>& Cephei::QL::Experimental::Varianceoption::CIntegralHestonVarianceOptionEngine::GetShared ()
{
    if (_ppIntegralHestonVarianceOptionEngine == NULL) throw gcnew NativeNullException ();
	return *_ppIntegralHestonVarianceOptionEngine;
}
QuantLib::IntegralHestonVarianceOptionEngine* Cephei::QL::Experimental::Varianceoption::CIntegralHestonVarianceOptionEngine::GetPointer ()
{
    if (_ppIntegralHestonVarianceOptionEngine == NULL) throw gcnew NativeNullException ();
	return &**_ppIntegralHestonVarianceOptionEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::IntegralHestonVarianceOptionEngine>& Cephei::QL::Experimental::Varianceoption::CIntegralHestonVarianceOptionEngine::GetHandle ()
{
	if (_phIntegralHestonVarianceOptionEngine == NULL)
	{
		_phIntegralHestonVarianceOptionEngine = new Handle<QuantLib::IntegralHestonVarianceOptionEngine> (*_ppIntegralHestonVarianceOptionEngine);
	}
	return *_phIntegralHestonVarianceOptionEngine;
}
#endif
bool Cephei::QL::Experimental::Varianceoption::CIntegralHestonVarianceOptionEngine::HasNative () 
{
	return (_ppIntegralHestonVarianceOptionEngine != NULL);
}

Cephei::QL::Experimental::Varianceoption::IIntegralHestonVarianceOptionEngine^ Cephei::QL::Experimental::Varianceoption::CIntegralHestonVarianceOptionEngine::Calculate::get ()
{
    try
    {
    	(*_ppIntegralHestonVarianceOptionEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Varianceoption::IIntegralHestonVarianceOptionEngine^ Cephei::QL::Experimental::Varianceoption::CIntegralHestonVarianceOptionEngine_Factory::Create (Cephei::QL::Processes::IHestonProcess^ process)
{
    return gcnew CIntegralHestonVarianceOptionEngine ( process);
}
